<?php
namespace Admin\Model;
use Think\Model;
class PartAreaModel extends Model{
    /**
     * 添加删除修改专业就业地区分布数据
     * @param $category 分类入口 专业，学校
     * @param int $majorId 专业id
     * @param int $schoolId 学校id
     * @param $rows  表单提交数据
     */
    function addCategoryPareArea($category,$majorId,$schoolId,$rows){
        switch($category){
            case "major":
                $partAreaData = $this->where("category = 'major' and major_id=" . $majorId)->select();
                    break;
            case "school":
                $partAreaData = $this->where("category = 'school' and school_id=" . $schoolId)->select();
                    break;
            case "both":
                $partAreaData = $this->where("category = 'both' and  major_id = ".$majorId." and school_id=" . $schoolId)->select();
                     break;
                return $partAreaData;
        }

        foreach ($rows as $k => $v) {
            foreach ($partAreaData as $key => $val) {
                if ($v['id'] == $val['id']) {
                    $data = array();
                    if ($v['area_id']) {
                        $data['area_id'] = $v['area_id'];
                    }
                    if ($v['bili'] != $val['bili']) {
                        $data['bili'] = $v['bili'];
                    }
                    if($v['bili'] > 100){
                        $return =  array("status" => 0, 'msg' => "编号为".$v['id']."的占比百分数不能超过100");
                    }
                    if (is_array($data) && count($data) >= 1) {
                        $res = $this->where("id=" . $v['id'])->save($data);
                    }
                }
            }
            if ($v['status']) {
                $data = array();
                $data['category'] = $category;
                $data['major_id'] = $majorId;
                $data['school_id'] = $schoolId;
                $data['area_id'] = $v['area_id'];
                $data['bili'] = $v['bili'];
                if($v['bili'] > 100){
                    $return =  array("status" => 0, 'msg' => "编号为".$v['id']."的占比百分数不能超过100");
                }
                $res = $this->add($data);
                if (!$res) {
                    $return =  array("status" => 0, 'msg' => "新增失败");
                }
            }
            $return =  array("status" => 1, 'msg' => "数据更新成功");
        }

        return $return;
    }

}